package com.ruoyi.framework.reptile.webmagic.jingcai.rank.nba;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.enums.JcTeamType;
import com.ruoyi.common.enums.Yn;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.reptile.webmagic.jingcai.AbstractJingCaiPipeline;
import com.ruoyi.framework.reptile.webmagic.jingcai.rank.cba.CbaRankResult;
import com.ruoyi.framework.reptile.webmagic.jingcai.rank.cba.SinaCbaRankPipeline;
import com.ruoyi.system.domain.JcTeam;
import com.ruoyi.system.service.IJcTeamService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dzl
 * @Description 新浪nba
 * @date 2024年07月18日 18:37:00
 */
public class SinaNbaRankPipeline extends AbstractJingCaiPipeline implements Pipeline {

    private static final Logger log = LoggerFactory.getLogger(SinaNbaRankPipeline.class);

    @Override
    public void process(ResultItems resultItems, Task task) {
        String jcData = resultItems.get("jcData").toString();
        NbaRankResult nbaRankResult = JSON.parseObject(jcData, NbaRankResult.class);
        log.info("nba球队排行榜爬取数据list:{}", JSON.toJSONString(nbaRankResult));
        IJcTeamService jcTeamService = getJcTeamService();
        String season = String.valueOf(nbaRankResult.getResult().getData().getLeague().getSeason());
        // 获取赛季
        if (StringUtils.isEmpty(season)) {
            log.info("season为空不处理，直接返回");
            return;
        }
        // 处理球队排名
        JcTeam queryParam = new JcTeam();
        queryParam.setSeason(season);
        queryParam.setType(JcTeamType.TYPE_NBA.getType());
        if (CollectionUtils.isNotEmpty(nbaRankResult.getResult().getData().getEastern())) {
            handleJcTeam(queryParam, buildUpdateList(nbaRankResult.getResult().getData().getEastern(), season));
        }

        if (CollectionUtils.isNotEmpty(nbaRankResult.getResult().getData().getWestern())) {
            // 处理球队排名
            handleJcTeam(queryParam, buildUpdateList(nbaRankResult.getResult().getData().getWestern(), season));
        }
    }

    /**
     * 构建更新的报文
     *
     * @param list
     * @return
     */
    private List<JcTeam> buildUpdateList(List<NbaRankTeam> list, String season) {
        List<JcTeam> jcTeamList = list.stream().map( nbaRankTeam -> {
            JcTeam jcTeam = new JcTeam();
            jcTeam.setTeam(nbaRankTeam.getTeam_name());
            jcTeam.setType(JcTeamType.TYPE_NBA.getType());
            jcTeam.setRanking(nbaRankTeam.getRank());
            jcTeam.setSeason(season);
            jcTeam.setHomeWins(nbaRankTeam.getHome_wins());
            jcTeam.setHomeLose(nbaRankTeam.getHome_losses());
            jcTeam.setOpponentWins(nbaRankTeam.getRoad_wins());
            jcTeam.setOpponentLose(nbaRankTeam.getRoad_losses());
            jcTeam.setAvgScores(new BigDecimal(nbaRankTeam.getPoints_for()));
            jcTeam.setAvgLoseScores(new BigDecimal(nbaRankTeam.getPoints_against()));
            jcTeam.setYn(Yn.ENABLE.getCode());
            jcTeam.setCreator("webmagic");
            jcTeam.setModifier("webmagic");
            return jcTeam;
        }).collect(Collectors.toList());
        return jcTeamList;
    }
}
